package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserLikeVo;
import com.itheima.service.mongo.UserLikeService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存用户喜欢信息
     * @param likeUserId 喜欢用户id
     * @param loginUserId 登录用户id
     */
    @Override
    public void saveUserLike(Long likeUserId, Long loginUserId) {
        //查询mongo中是否有该条数据
        Query query = new Query(Criteria.where("userId").is(loginUserId)
                                .and("likeUserId").is(likeUserId));
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        //如果没有,执行保存操作
        if (userLike == null) {
            userLike = new UserLike();
            userLike.setUserId(loginUserId);
            userLike.setLikeUserId(likeUserId);
            userLike.setCreated(System.currentTimeMillis());
            mongoTemplate.save(userLike);
        }
        //删除推荐数据
        Query query1 = new Query(Criteria.where("toUserId").is(loginUserId)
                                .and("userId").is(likeUserId));
        mongoTemplate.remove(query1, RecommendUser.class);
    }

    /**
     * 判断两个用户是否互相喜欢
     * @param loginUserId 登录用户
     * @param likeUserId 喜欢用户
     * @return 是否相互喜欢
     */
    @Override
    public boolean likeEachOther(Long loginUserId, Long likeUserId) {
        Query query1 = new Query(Criteria.where("userId").is(loginUserId)
                                .and("likeUserId").is(likeUserId));
        UserLike userLike = mongoTemplate.findOne(query1, UserLike.class);
        Query query2 = new Query(Criteria.where("userId").is(likeUserId)
                .and("likeUserId").is(loginUserId));
        UserLike likeUser = mongoTemplate.findOne(query2, UserLike.class);
        return userLike != null && likeUser != null;
    }

    /**
     * 删除我对该用户的喜欢
     * @param loginUserId 登录用户id
     * @param unLoveId 不喜欢用户id
     */
    @Override
    public void deleteUserLike(Long loginUserId, Long unLoveId) {
        Query query = new Query(Criteria.where("userId").is(loginUserId)
                                .and("likeUserId").is(unLoveId));
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        if (userLike != null) {
            mongoTemplate.remove(userLike);
        }
        Query query1 = new Query(Criteria.where("toUserId").is(loginUserId)
                .and("userId").is(unLoveId));
        mongoTemplate.remove(query1, RecommendUser.class);
    }

    /**
     * 查询互相喜欢
     * @param loginUserId 登录用户id
     * @return 互相喜欢数量
     */
    @Override
    public Long eachLoveCount(Long loginUserId) {
        Query query = new Query(Criteria.where("userId").is(loginUserId));
        return mongoTemplate.count(query, Friend.class);
    }

    /**
     * 查询我的喜欢
     * @param loginUserId 登录用户
     * @return 我的喜欢数量
     */
    @Override
    public Long LoveCount(Long loginUserId) {
        return mongoTemplate.count(new Query(Criteria.where("userId").is(loginUserId)),UserLike.class);

    }

    /**
     * 查询我粉丝
     * @param loginUserId 登录用户
     * @return 我的粉丝数量
     */
    @Override
    public Long fanCount(Long loginUserId) {
        return mongoTemplate.count(new Query(Criteria.where("likeUserId").is(loginUserId)),UserLike.class);
    }

    /**
     * 查询相互喜欢/喜欢/粉丝/谁看过我详细信息
     * @param page 页码
     * @param pageSize 每页条数
     * @param loginUserId 登录用户id
     * @param type 操作类型
     * @return
     */
    @Override
    public PageBeanVo findUserListByType(Integer page, Integer pageSize, Long loginUserId, String type) {
        int startIndex = (page - 1) * pageSize;
        long count = 0L;
        Query query = null;
        List<UserLikeVo> voList = new ArrayList<>();
        //查询相互喜欢用户
        if (StrUtil.equals(type, "1")) {
            query = new Query(Criteria.where("userId").is(loginUserId))
                    .with(Sort.by(Sort.Order.desc("created")))
                    .skip(startIndex).limit(pageSize);
            count = mongoTemplate.count(query, Friend.class);
            List<Friend> friendList = mongoTemplate.find(query, Friend.class);
            if (CollectionUtil.isNotEmpty(friendList)) {
                for (Friend friend : friendList) {
                    UserLikeVo likeVo = transferToUserLikeVo(friend.getFriendId());
                    voList.add(likeVo);
                }
            }
            return new PageBeanVo(page, pageSize, count, voList);
        //查询我的喜欢
        } else if (StrUtil.equals(type, "2")) {
            query = new Query(Criteria.where("userId").is(loginUserId))
                    .with(Sort.by(Sort.Order.desc("created")))
                    .skip(startIndex).limit(pageSize);
            count = mongoTemplate.count(query, UserLike.class);
            List<UserLike> likeList = mongoTemplate.find(query, UserLike.class);
            if (CollectionUtil.isNotEmpty(likeList)) {
                for (UserLike userLike : likeList) {
                    UserLikeVo userLikeVo = transferToUserLikeVo(userLike.getLikeUserId());
                    voList.add(userLikeVo);
                }
            }
            return new PageBeanVo(page, pageSize, count, voList);
        //查询我的粉丝
        } else if (StrUtil.equals(type, "3")) {
            query = new Query(Criteria.where("likeUserId").is(loginUserId))
                    .with(Sort.by(Sort.Order.desc("created")))
                    .skip(startIndex).limit(pageSize);
            count = mongoTemplate.count(query, UserLike.class);
            List<UserLike> likeList = mongoTemplate.find(query, UserLike.class);
            if (CollectionUtil.isNotEmpty(likeList)) {
                for (UserLike userLike : likeList) {
                    UserLikeVo userLikeVo = transferToUserLikeVo(userLike.getUserId());
                    voList.add(userLikeVo);
                }
            }
            return new PageBeanVo(page, pageSize, count, voList);
        //查询谁访问过我
        } else if (StrUtil.equals(type, "4")) {
            query = new Query(Criteria.where("userId").is(loginUserId))
                    .with(Sort.by(Sort.Order.desc("created")))
                    .skip(startIndex).limit(pageSize);
            count = mongoTemplate.count(query, Visitor.class);
            List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
            if (CollectionUtil.isNotEmpty(visitorList)) {
                for (Visitor visitor : visitorList) {
                    UserLikeVo userLikeVo = transferToUserLikeVo(visitor.getVisitorUserId());
                    voList.add(userLikeVo);
                }
            }
            return new PageBeanVo(page, pageSize, count, voList);
        }
        return null;
    }

    /**
     * 将id转换为userLikeVo对象
     * @param userId 用户id
     * @return userLikeVo对象
     */
    public UserLikeVo transferToUserLikeVo(Long userId) {
        UserLikeVo userLikeVo = new UserLikeVo();
        userLikeVo.setId(userId);
        return userLikeVo;
    }
}
